package com.fastmobile.impl;

import java.text.ParseException;
import java.util.List;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.Transaction;

import org.json.JSONException;
import org.json.JSONObject;

import com.fastmobile.entity.ContactsList;
import com.fastmobile.entity.Departments;
import com.fastmobile.entity.HAWB;
import com.fastmobile.entity.InvoiceReference;
import com.fastmobile.entity.TransactionDetails;
import com.fastmobile.entity.Transactions;

public class UT {
	public static void main(String[] args) throws ParseException, JSONException {
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("com.fastmobile");
		EntityManager em = emf.createEntityManager();
		
		TransactionsImpl transactionImpl = new TransactionsImpl();
		transactionImpl.setEntityManager(em);
		Date today = new Date();
//		transactionImpl.unlockDocumentTransactionById("S0419/11", today, "test");
		
//		transactionImpl.changeJobCreator("S0419/11", "Tho", today, "test");
//		Transactions transaction =  transactionImpl.getTransactionById("S0419/11");
//		System.out.println("ID: " + transaction.getTransactionId());
//		System.out.println("Transaction Unlock: " + transaction.isTransactionUnlock());
//		System.out.println("Date of Prealert: " + transaction.getDateofPrealert());
//		System.out.println("Notes: " + transaction.getTransactionNotes());		
//		System.out.println("Owner: " + transaction.getWhoisMaking());
		
//		TransactionDetails transDetail = transactionImpl.getDetailTransactionsById("S0419/11");
//		HAWB hwab = transactionImpl.getHAWBById(transDetail.getHWBNO());
//		System.out.println("Owner: " + hwab.getOwner());
//		
		InvoiceReferenceImpl invoiceReferenceImpl = new InvoiceReferenceImpl();
		invoiceReferenceImpl.setEntityManager(em);
//		invoiceReferenceImpl.lockInvoice("000071", today, "test");
		
//		InvoiceReference invoice = invoiceReferenceImpl.getInvoiceReferenceById("000071");
//		System.out.println("Notes: " + invoice.getNotes());
//		System.out.println("Date of Lock: " + invoice.getDateOfLock());
		
		ContactsListImpl contactsListImpl = new ContactsListImpl();
		contactsListImpl.setEntityManager(em);
//		JSONObject obj = contactsListImpl.getJsonContactByUserNameAndPassWord("nancy", "123456");
//		int accessRight = (Integer) obj.get("accessRight");
//		System.out.println(accessRight);
//		System.out.println(obj.toString());
//		List<ContactsList> list = contactsListImpl.getAllContactsList();
//		for (ContactsList contact: list) {
//			System.out.println("Id: " + contact.getContactId());
//			System.out.println("Username: " + contact.getUserName());
//		}
		
//		List<ContactsList> contactsList = contactsListImpl.getContacsListByDeptId("DP006");
//		for (ContactsList contact:contactsList) {
//			System.out.println("Id: " + contact.getContactId());
//			System.out.println("Username: " + contact.getUserName());
//			System.out.println("Dept Id: " + contact.getDeptId());
//			System.out.println("AccessRight: " + contact.getAccessRight());
//		}
//		List<JSONObject> listJson = contactsListImpl.getJsonContactsList();
//		for (JSONObject json:listJson) {
//			System.out.println(json);
//		}
		
		List<ContactsList> contacts = (List<ContactsList>)contactsListImpl.getContacsListByDeptId("DP005");
		
		if (null != contacts && contacts.size() > 0) {
			for (ContactsList contact : contacts) {
				System.out.println("ContactsList: " + contact.getContactId()); 
				List<Departments> depts = contact.getDepts();
				if (null != depts && depts.size() > 0) {
					for (Departments departments : depts) {
						System.out.println("DeptID: " + departments.getDeptId());
					}
				}
			}
		}
		
		
//		List<Object[]> listObject = contactsListImpl.getDetail("DP005");
//		for (Object obj:listObject) {
//			System.out.println(obj);
//		}
		System.out.println("=====================Test===================");
//		List<Transactions> list = transactionImpl.getAllTransaction();
//		List<Transactions> list = transactionImpl.getLockedTransactionByQuantity(10);
//		if (null != list && list.size() > 0) {
//			for (Transactions trans : list) {
//				System.out.println("ID: " + trans.getTransactionId());
//				System.out.println("Transaction Unlock: " + trans.isTransactionUnlock());
//				System.out.println("Date of Prealert: " + trans.getDateofPrealert());
//				System.out.println("-----------------");
//			}
//		}
		
		
	}
}
